package com.roncoo.permission.config;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.roncoo.permission.entity.Message;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @author scf
 * @Date 2020/12/9
 */

public class LoginFilter extends FormAuthenticationFilter {

    /**
     * 前端没有登录就开始访问   我们的路径
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        //返回json数据
        HttpServletResponse resp=(HttpServletResponse)response;
        resp.setHeader("Access-Control-Allow-Origin","*");
        resp.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        //Message对象
        Message msg=new Message(1,"没有登录");
        System.out.println(msg.toString());
        //msg对象   传出去  json数据
       // out.write("您还没有登录！！！");
        String s = objectMapper.writeValueAsString(msg);
        out.write(s);
        return false;
    }
}
